

// 切换菜单
$('.Baby_Healthy_home_billing_left_dl>dd').on("click",function(){
    if($(this).attr('flage')=='true'){
        $(this).addClass("bgc_alert").siblings().removeClass("bgc_alert");
        var ddIndex = $(this).index();
        $('.js_cardListgroup').removeClass('hidden')
        $('.js_nocontent').addClass('hidden')
        if(ddIndex=="1"){
            $('.js_cardListgroup').find('.Baby_Healthy_home_billing_right_center_tit').text('储值卡')
            filterCardList(4)
        }else if(ddIndex=="2"){
            $('.js_cardListgroup').find('.Baby_Healthy_home_billing_right_center_tit').text('计次卡')
            filterCardList(5)
        }
    }
});


// 清空全部
$('.consumeListContains').on('click',".clearAll",function(){
    $('.Baby_Healthy_home_billing_left_dd').attr('flage',false).removeClass('bgc_alert');
    $(this).parents(".consumeListContains").addClass('hidden').siblings('.serachSelect').removeClass('hidden').siblings('.noticeImg').removeClass('hidden');
    $('.Baby_Healthy_home_billing_right_center').eq(0).removeClass('hidden').siblings(".Baby_Healthy_home_billing_right_center").addClass("hidden");
    $('.js_storeelm').find('.storeelm_checkbox').removeClass('checkBox_checked')
});
var orderDialog=''
//保存订单
$('.consumeListContains').on('click','.saveBtnPay',function(){
    var $this = $(this)
    if($(this).attr('rel') == 1){
        baseFn.infoDialog('订单正在提交中，不要重复操作')
        return false
    }else{
        $(this).attr('rel',1)
    }
    orderDialog=baseFn.dialog({
        content:'确认保存该订单？',
        ok:function(){
            DocData.saveOrder(true,'.saveBtnPay')
        },
        cancel:function(){
            $this.attr('rel',0)
        }
    })
})
//收款
$('.consumeListContains').on('click','.gatheringBtn',function(){
    //收款
    var $this = $(this)
    if(DocData.orderId != ''){
        if(DocData.canPay){
                $.get(baseUrl.base+baseUrl.order+'selectOrderInfo',{orderId:DocData.orderId},function(res){
                    if(res.code==0){
                        if($this.attr('rel') == 1){
                            baseFn.infoDialog('正在付款中，不要重复操作')
                            return false
                        }else{
                            $this.attr('rel',1)
                        }
                        DocData.initOrder(res.results)
                        DocData.orderData = res.results
                        DocData.couponId = res.results.orderInfo.couponId
                        if(DocData.otherPay && time != ''){
                            //代付
                            $('.js_payOrderPhone').val('')
                            $('.js_checkId').val('')
                            window.clearInterval(time)
                            $('.js_getCheckBtn').removeClass('disabled').text('获取验证码')
                            time = '';
                            _time = 60;
                        }
                        baseFn.dialog({
                            width:'40em',
                            height:'65vh',
                            title:'收银台',
                            content:$('.gatheringlist'),
                            okValue:'确认收款',
                            ok:function(){
                                var _upDatas={
                                    orderId:DocData.orderId,
                                    cashierId:DocData.cashierId
                                }
                                if(DocData.otherPay){
                                    var _txt=''
                                    _upDatas.check = $('input.js_checkId').val()
                                    _upDatas.userPhone = $('input.js_payOrderPhone').val()
                                    // if(!baseFn.notNull(_upDatas.check)){
                                    //     _txt = '请输入验证码'
                                    // }else
                                    if(!baseFn.notNull(_upDatas.userPhone)){
                                        _txt = '请输入代付人手机号'
                                    }else if(!/^13[0-9]{9}$|14[0-9]{9}|17[0-9]{9}|15[0-9]{9}$|18[0-9]{9}$/.test(_upDatas.userPhone)){
                                        _txt = '请输入正确的手机号格式'
                                    }
                                    if(baseFn.notNull(_txt)){
                                        baseFn.infoDialog(_txt)
                                        return false
                                    }
                                }
                                _upDatas.payWay = 6;
                                if(baseFn.notNull(DocData.payWays) && !baseFn.notNull(DocData.payUserId)){
                                    _upDatas.payWays = DocData.payWays
                                }
                                if(baseFn.notNull(DocData.payWays) && DocData.otherPay){
                                    _upDatas.payWays = 7
                                }
                                
                                $.post(baseUrl.base+baseUrl.order+'submitOrderInfo',_upDatas,function(res){
                                    if(res.code ==0){
                                        $this.attr('rel',0)
                                        var _rtTxt='支付完成';
                                        if(DocData.payGoods != 0){
                                            DocData.userInfo.userAccount = (Number(DocData.userInfo.userAccount) - Number(DocData.payGoods))
                                            _rtTxt = '支付完成，账户余额还有'+DocData.userInfo.userAccount +'元。'
                                            DocData.payGoods = 0
                                        }
                                        baseFn.infoDialog(_rtTxt,function(){
                                            window.location.href=DocData.orderURL
                                            baseFn.goPath('gotoOrder',{flag:3})
                                        })

                                    }else if(res.code == 60007){
                                        // baseFn.dialog({
                                        //     title:'提示',
                                        //     content:'该订单优惠券已用，请更新订单后付款'
                                        // })
                                        baseFn.infoDialog('该订单优惠券已用，请更新订单后付款')
                                        $this.attr('rel',0)
                                        couponlist();
                                        DocData.canPay=false
                                        DocData.couponId='';
                                        DocData.orderData.orderInfo.couponId=''
                                    }else{
                                        $this.attr('rel',0)
                                        baseFn.infoDialog(res.msg)
                                    }
                                },'json').complete(function(){
                                    $this.attr('rel',0)
                                })
                            },
                            cancel:function(){
                                $this.attr('rel',0)
                            }
                        })
                        DocData.canRenderOrderList(2)
                    }else{
                        baseFn.infoDialog(res.msg)
                    }
                },'json')

        }else{
            var d=baseFn.dialog({
                content:'订单有修改，是否保存后进行收款？',
                ok:function(){
                    DocData.saveOrder(true,'.saveBtnPay')
                },
                cancel:function(){}
            })
        }
    }else{
        var d=baseFn.dialog({
            content:'订单未保存，是否保存后进行收款？',
            ok:function(){
                DocData.saveOrder(true,'.saveBtnPay')
            },
            cancel:function(){}
        })
    }
})
$('.payelm').on('click',function(){
    $(this).parents('.zccardelm').find('.payelm').removeClass('active')
    $(this).addClass('active')
    DocData.payWays=$(this).attr('rel')
})
$('.consumeListContains').on('change','.js_orderDesc',function(){
    if(baseFn.notNull(DocData.orderId)){
        DocData.canPay = false
    }
})
baseFn.powerFn()
//代付按钮
$('.consumeListContains').on('click','.otherpay_wrap_btn',function(){
    
    if($(this).hasClass('active')){
        $(this).removeClass('active')
        DocData.otherPay = false
        DocData.payWay = 0
        DocData.payWays = 0
        $('.payelm').removeClass('active')
        $('.payelm[rel="3"]').addClass('active')
    }else{
        $(this).addClass('active');
        DocData.otherPay = true;
        DocData.payWay = 7
        DocData.payWays = 7
        
    }  
})
//获取验证码
var time = '',_time=60
$('.js_getCheckBtn').on('click',function(){
    var _upDatas={
        orderId:DocData.orderId,
        userPhone:$('input.js_payOrderPhone').val()
    }
    var _txt=''
    if(!baseFn.notNull(_upDatas.orderId)){
        _txt = '没有订单，请先保存订单'
    }else if(!baseFn.notNull(_upDatas.userPhone)){
        _txt = '请输入代付人手机号'
    }else if(!/^13[0-9]{9}$|14[0-9]{9}|17[0-9]{9}|15[0-9]{9}$|18[0-9]{9}$/.test(_upDatas.userPhone)){
        _txt = '请输入正确的手机号格式'
    }
    if(baseFn.notNull(_txt)){
        baseFn.infoDialog(_txt)
        return false
    }
    var $this = $(this)
    if($this.hasClass('disabled')){
        return false
    }else{
        baseFn.$post({
            url:'order/replacePayOrder',
            data:_upDatas,
            canNull:true,
            calFn:function(res){
                baseFn.infoDialog('成功发送验证码')
                $this.addClass('disabled')
                time = setInterval(function(){
                    $this.text('还有'+_time+'秒')
                    _time --
                    if(_time == '0'){
                        _time = 60;
                        $this.removeClass('disabled').text('重新获取')
                        window.clearInterval(time)
                        time=''
                    }
                },1000)
            }
        })
    } 
})
